class Radar {
  constructor() {
    this.timer = null
    this.markers = [
      {
        id: 'target1',
        coordinate: [
          [529463.63, 2501253.53, 100],
          [529663.78, 2501507.19, 100],
          [529734.08, 2501685.44, 100],
          [529828.28, 2502045.36, 100],
          [529925.04, 2502225.93, 100],
          [530114.93, 2502317.33, 100],
          [530303.02, 2502375.2, 100],
          [530476.65, 2502433.08, 100],
          [530551.48, 2502517.29, 100],
          [530806.52, 2502612.48, 100]
        ],
        RCS: [0.03, 0.02, 0.04],
        speed: [15.1, 15.2, 15.4],
        height: [89, 93, 94],
        target: '鸟类'
      },
      {
        id: 'target2',
        coordinate: [
          [534277.35, 2498000.62, 100],
          [534147.65, 2498260.02, 100],
          [534178.37, 2498473.56, 100],
          [534174.82, 2498763.13, 100],
          [534112.71, 2498923.8, 100],
          [533955.77, 2499110.44, 100],
          [533920.15, 2499224.43, 100],
          [533931.9, 2499455.81, 100],
          [533898.78, 2499604.38, 100],
          [533859.75, 2499763.66, 100]
        ],
        RCS: [0.41, 0.44, 0.45],
        speed: [7.7, 7.8, 8.1],
        height: [188, 191, 197],
        target: '滑翔伞'
      },
      {
        id: 'target3',
        coordinate: [
          [534044.56, 2502578.9, 100],
          [533946.44, 2502328.67, 100],
          [533871.34, 2502115.89, 100],
          [533790.9, 2501976.46, 100],
          [533796.24, 2501759.18, 100],
          [533746.18, 2501583.96, 100],
          [533646.05, 2501396.22, 100],
          [533570.95, 2501271.06, 100],
          [533513.83, 2500993.78, 100],
          [533529.67, 2500738.39, 100]
        ],

        RCS: [0.11, 0.12, 0.14],
        speed: [8.9, 9.4, 9.1],
        height: [52, 48, 47],
        target: '无人机'
      },
      {
        id: 'target4',
        coordinate: [
          [532972.67, 2500121.82, 100],
          [533162.45, 2500219.38, 100],
          [533369.77, 2500268.16, 100],
          [533589.29, 2500316.94, 100],
          [533731.44, 2500414.51, 100],
          [533994.01, 2500512.07, 100],
          [534184.56, 2500463.29, 100],
          [534326.95, 2500454.02, 100],
          [534474.45, 2500357.7, 100],
          [534590.82, 2500316.94, 100]
        ],
        RCS: [0.02, 0.03, 0.03],
        speed: [15.6, 15.8, 15.5],
        height: [159, 163, 154],
        target: '鸟类'
      },
      {
        id: 'target5',
        coordinate: [
          [530543.62, 2500232.22, 100],
          [530688.73, 2500282.28, 100],
          [530788.47, 2500382.41, 100],
          [530928.88, 2500382.41, 100],
          [531145.96, 2500432.48, 100],
          [531253.91, 2500282.28, 100],
          [531115.45, 2500132.09, 100],
          [530958.22, 2500132.09, 100],
          [530796.29, 2500031.96, 100],
          [530630.84, 2499931.83, 100]
        ],
        RCS: [0.04, 0.04, 0.02],
        speed: [16.1, 16.8, 17.1],
        height: [279, 283, 284],
        target: '鸟类'
      },
      {
        id: 'target6',

        coordinate: [
          [532508.335, 2497306, 100],
          [532419.63, 2497723.16, 100],
          [532421.49, 2498031.98, 100],
          [532628.33, 2498285.96, 100],
          [532754.19, 2498502.53, 100],
          [532725.69, 2498768.49, 100],
          [532674.03, 2498917.06, 100],
          [532606.96, 2499148.44, 100],
          [532589.85, 2499378.84, 100],
          [532534.53, 2499699.86, 100]
        ],
        RCS: [0.23, 0.22, 0.25],
        speed: [10.3, 10.9, 11.1],
        height: [112, 118, 117],
        target: '无人机'
      },
      {
        id: 'target7',
        coordinate: [
          [530641.05, 2501692.16, 100],
          [530764.58, 2501840.4, 100],
          [530925.17, 2501939.22, 100],
          [531048.7, 2501988.64, 100],
          [531184.59, 2501988.64, 100],
          [531295.77, 2501988.64, 100],
          [531419.3, 2502087.46, 100],
          [531629.3, 2502136.87, 100],
          [531728.13, 2502235.7, 100],
          [531789.9, 2502235.7, 100]
        ],
        RCS: [0.05, 0.06, 0.05],
        speed: [15.6, 15.7, 15.8],
        height: [68, 69, 72],
        target: '鸟类'
      },
      {
        id: 'target8',
        coordinate: [
          [532502.8, 2503034.8, 100],
          [532497.24, 2502667.69, 100],
          [532448.56, 2502508.12, 100],
          [532397.15, 2502398.86, 100],
          [532421.14, 2502204.76, 100],
          [532421.14, 2502027.19, 100],
          [532446.51, 2501792.56, 100],
          [532619.73, 2501634.02, 100],
          [532725.53, 2501488.17, 100],
          [532870.05, 2501327.38, 100]
        ],
        RCS: [0.07, 0.07, 0.08],
        speed: [17.1, 16.9, 17.2],
        height: [224, 237, 229],
        target: '鸟类'
      },
      {
        id: 'target9',
        coordinate: [
          [531412.12, 2498789.46, 100],
          [531632.39, 2498869.56, 100],
          [531897.21, 2498661.3, 100],
          [532063.28, 2498406.77, 100],
          [532082.57, 2498175.38, 100],
          [531947.59, 2497866.85, 100],
          [531774.04, 2497635.46, 100],
          [531696.91, 2497481.2, 100],
          [531705.97, 2497163.39, 100],
          [531630.7, 2496862.3, 100]
        ],
        RCS: [12.21, 12.35, 12.3],
        speed: [42.1, 43.5, 43.2],
        height: [368, 373, 375],
        target: '直升机'
      },
      {
        id: 'target10',
        coordinate: [
          [529538.2, 2498701.08, 100],
          [529502.41, 2498971.83, 100],
          [529562.59, 2499201.08, 100],
          [529540.77, 2499468.74, 100],
          [529515.74, 2499681.51, 100],
          [529540.77, 2499900.54, 100],
          [529504.06, 2500131.09, 100],
          [529565.8, 2500344.86, 100],
          [529681.62, 2500682.8, 100],
          [529884.54, 2500758.9, 100]
        ],
        RCS: [0.05, 0.06, 0.06],
        speed: [16.3, 15.9, 16.4],
        height: [139, 144, 142],
        target: '鸟类'
      }
    ]
  }

  random(m, n) {
    return parseInt(m + Math.random() * (n - m))
  }

  startAnimation02() {
    let i = 0
    const RCSARR = [0.01, 0.012, 0.013, 0.015]
    let allmarkers = []
    let updateMarkers = []
    let nthmarkers = [] //添加了第一几批，用来做删除
    this.timer = setInterval(async () => {
      let markerArr = []
      allmarkers.forEach((item) => {
        updateMarkers.push({
          id: item.id,
          textRange: [0, 0]
        })
      })
      // await __g.marker.deleteByGroupId('RadarMarker')
      await __g.marker.update(updateMarkers)
      this.markers.forEach((item, index) => {
        let o = {
          id: item.id + '-' + i, // maker唯一标识
          groupId: 'RadarMarker',
          coordinate: item.coordinate[i], //坐标位置
          coordinateType: 0, // 坐标系类型
          imagePath: window.origin + '/poi/wrj.png', //图片路径
          anchors: [-10, 20],
          imageSize: [20, 20],
          fixedSize: true,
          text: `目标：目标${index + 1}\rRCS：${item.RCS[this.random(0, 3)]}m²\r高度：${item.height[this.random(0, 3)]}m\r速度：${item.speed[this.random(0, 3)]}m/s\r参考目标：${item.target}`,
          fontColor: '#ffffff',
          fontSize: 10,
          textBackgroundColor: '#1a918b',
          range: [0, 20000000], //标签的可见范围 [Min,Max]
          textRange: [0, 2000000000],
          autoHidePopupWindow: true,
          displayMode: 2, //显示模式
          priority: 0, //避让优先级
          userData: JSON.stringify(item.userData),
          autoHeight: false,
          occlusionCull: false
        }
        markerArr.push(o)
        allmarkers.push(o)
      })
      i++
      __g.marker.add(markerArr, null)
      nthmarkers.push(markerArr)
      let delIds = []
      if (nthmarkers.length > 3) {
        let newarr = nthmarkers.splice(0, 1)
        newarr.forEach((item) => {
          item.forEach((item02) => {
            delIds.push(item02.id)
          })
        })
        __g.marker.delete(delIds)
      }
      if (i > 10) {
        i = 0
        // clearInterval(this.timer)
      }
    }, 2000)
  }

  startAnimation() {
    let i = 0
    let markerArr = []
    let updateMarkers = []
    this.timer = setInterval(async () => {
      if (i == 0) {
        this.markers.forEach((item, index) => {
          let o = {
            id: item.id, // maker唯一标识
            groupId: 'RadarMarker',
            coordinate: item.coordinate[i], //坐标位置
            coordinateType: 0, // 坐标系类型
            imagePath: window.origin + '/poi/wrj.png', //图片路径
            anchors: [-10, 20],
            imageSize: [16, 16],
            fixedSize: true,
            text: `目标：目标${index + 1}\rRCS：${item.RCS[this.random(0, 3)]}m²\r高度：${item.height[this.random(0, 3)]}m\r速度：${item.speed[this.random(0, 3)]}m/s\r参考目标：${item.target}`,
            fontColor: '#ffffff',
            fontSize: 8,
            textBackgroundColor: [6 / 255, 185 / 255, 33 / 255, 0.6],
            range: [0, 20000000], //标签的可见范围 [Min,Max]
            textRange: [0, 2000000000],
            autoHidePopupWindow: true,
            displayMode: 2, //显示模式
            priority: 0, //避让优先级
            userData: JSON.stringify(item.userData),
            autoHeight: false,
            occlusionCull: false
          }
          markerArr.push(o)
        })
        // __g.marker.add(markerArr, null)
      } else {
        this.markers.forEach((item) => {
          updateMarkers.push({
            id: item.id,
            coordinate: item.coordinate[i]
          })
        })
        __g.marker.update(updateMarkers)
      }
      i++
      if (i > 10) {
        i = 0
      }
    }, 2000)
  }

  clear() {
    clearInterval(this.timer)
    __g.marker.deleteByGroupId('RadarMarker')
  }
}

export default new Radar()
